home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CWL2A_1.ARJ / DEMO.C < prev    next >
C/C++ Source or Header  |  1991-11-16  |  33KB  |  1,056 lines

  1. /* Date 11-1-91 */
  2.  
  3. /*********************************************************************
  4.  * This is the demonstration program of some of the functions        *
  5.  * available with The C Window Library.                              *
  6.  *                                                                   *
  7.  * For Turbo C version 1.5 and above,  compile using the following:  *
  8.  *     tcc -mm -Ixxx demo.c tcwinm.lib                               *
  9.  * where xxx is the directory of The C Window Library header files.  *
  10.  *                                                                   *
  11.  * For Microsoft C users Version 5.1 and above:                      *
  12.  *     cl -AM -Ixxx -DMSC demo /link /SE:1000 mswinm.lib             *
  13.  * (use "qcl" instead of "cl" if using Quick C)                      *
  14.  * where  xxx is the directory of The C Window Library header files. *
  15.  *                                                                   *
  16.  * For Zortech C++ Version 2.0 and above:                            *
  17.  *     ztc -mm -Ixxx -p -b demo.c zcwinm.lib                         *
  18.  * where  xxx is the directory of The C Window Library header files. *
  19.  *                                                                   *
  20.  * For Power C Version 2.1 and above:                                *
  21.  *     pc -DPOWERC -mm -c -ixxx demo.c                               *
  22.  *     pcl [5k,45k,300k] demo,pcwinm                                 *
  23.  * where  xxx is the directory of The C Window Library header files. *
  24.  *********************************************************************/
  25.  
  26. #include "window.h"
  27. #include <stdio.h>
  28. #include <dos.h>
  29.  
  30.  
  31. int approx_delay(int);
  32. void demo(void);
  33. int write_all(WPOINTER);
  34. void slide_window(WPOINTER);
  35. int slide_windows(void);
  36. int change_screen_size(void);
  37. int read_file(void);
  38. int pan_window(WPOINTER);
  39. void resize_demo(void);
  40. void last_function(void);
  41. void slide_see_thru_window(WPOINTER);
  42. void initialize_error(void);
  43. void custom_error_func(int errcode, char *sourcefile, int sourceline,
  44.                char *funcname);
  45.  
  46. /* Define colors used */
  47.  
  48. #define YELLOWONRED    CREATE_VIDEO_ATTRIBUTE(red,yellow)
  49. #define WHITEONRED     CREATE_VIDEO_ATTRIBUTE(red,lightwhite)
  50. #define WHITEONBLUE    CREATE_VIDEO_ATTRIBUTE(blue,white)
  51. #define WHITEONBLACK   CREATE_VIDEO_ATTRIBUTE(black,gray)
  52. #define GREENONBLACK   CREATE_VIDEO_ATTRIBUTE(black,green)
  53. #define BLACKONWHITE   CREATE_VIDEO_ATTRIBUTE(white,black)
  54. #define WHITEONGREEN   CREATE_VIDEO_ATTRIBUTE(green,white)
  55. #define GRAYONBLACK    CREATE_VIDEO_ATTRIBUTE(black,gray)
  56. #define GREENONBLUE    CREATE_VIDEO_ATTRIBUTE(blue,green)
  57. #define NORM           CREATE_VIDEO_ATTRIBUTE(black,white)
  58.  
  59.  
  60. /* define filename characters */
  61.  
  62. #define FILENAMECHARS  "25[A-Za-z0-9!@#$%/^&()+=_/-{}/[/]`~/.\\:]"
  63.  
  64.  
  65. /* define titles used for each window */
  66.  
  67. char *title1 = " [ Main Window ] ";
  68. char *title2 = " [ Window 1 ] ";
  69. char *title3 = " [ Window 2 ] ";
  70. char *title4 = " [ Window 3 ] ";
  71.  
  72.  
  73. char filename[26];     /* filename buffer */
  74. FILE *infile;          /* input file handle */
  75.  
  76.  
  77. /* Text written to main window */
  78.  
  79. char *screen1[] = {
  80.   " Welcome to the demonstration of the C Window Library!!  The C Window Library",
  81.   " is a collection of C functions that manipulate text windows.  With these    ",
  82.   " text windows, you can ...",
  83. NULL};
  84.  
  85. char *effectstr[] = {"Normal...",
  86.                      "Normal Explode...",
  87.                      "Contract...",
  88.              "Bricks...",
  89.              "Explode From Center To Top And Bottom...",
  90.              "Explode To Center From Top And Bottom...",
  91.              "Explode From Top...",
  92.              "Explode From Bottom...",
  93.              "Explode From Center To Left And Right...",
  94.              "Explode From Left And Right To Center...",
  95.              "Explode From Left To Right...",
  96.              "Explode From Right To Left..."
  97.              };
  98.  
  99. char *delay_text[] = {
  100.   "*  Create Windows",
  101.   "*  Write Text to Windows",
  102.   "*  Hide Windows",
  103.   "*  Resize Windows",
  104.   "*  Get Input From Windows",
  105.   "*  Scroll Up or Down in Windows",
  106.   "*  Change Colors on the fly",
  107.   "*  Create Shadows",
  108.   "*  Create See-Thru Windows",
  109.   "*  Border and Unborder Windows",
  110.   "*  Use Windows Larger than the Physical Screen",
  111.   "*  Create Pop-up, Bar, and PullDown Menus",
  112.   "*  And Much More!",
  113. NULL};
  114.  
  115.  
  116. char *closing_message[] = {
  117. "These are just a sample of the many things you can do with",
  118. "The C Window Library!",
  119. "The C Window Library is guaranteed to cut development time down substantially",
  120. "without putting a lot of strain on your budget!",
  121. " ",
  122. "The full registered version of The C Window Library is only $50. ",
  123. "With the full registered version you will receive technical support, free ",
  124. "upgrades to the next major version, and the full source code.  To receive ",
  125. "the full registered version send check or money order for $50 to",
  126. "  ",
  127. "P. A. McKenzie",
  128. "CitySoft Software",
  129. "100-3 Alcott Place",
  130. "Bronx, NY 10475",
  131. "  ",
  132. "Thank you for running the demo of The C Window Library!!",
  133.   };
  134.  
  135.  
  136.  
  137.  
  138.  
  139. WPOINTER main_window,              /* Main Window */
  140.          help,                     /* Help Window */
  141.          esc_window,               /* Escape Window */
  142.          error_window;             /* Error Window */
  143.  
  144. WPOINTER w[3];                     /* Window Pointers to miscellaneous
  145.                                     * windows */
  146.  
  147. VWPOINTER vw;                      /* Virtual window pointer */
  148.  
  149. VBLOCKPTR v;                       /* Video Block Pointer */
  150.  
  151. int error_check_flag = 1;          /* Turn error checking on (not to be
  152.                                     * confused with the internal variable
  153.                                     * check_window_error) */
  154.  
  155. void main()
  156. {
  157.   int i,d;
  158.   int initrow,initcol;             /* Initial row and column of the cursor */
  159.  
  160.   printf("\nPlease enter: '0' for Direct Screen Writes\n"
  161.      "              '1' for Direct Screen Writes with snow checking\n"
  162.      "              '2' for BIOS Screen updates: ");
  163.   scanf("%d",&d);
  164.  
  165.   /* Get the cursor Position */
  166.   GetCursorPosition(&initrow,&initcol,0);
  167.  
  168.   /* Initialize The C Window Library */
  169.   WindowInitializeSystem();
  170.  
  171.   /* Initialize error function */
  172.   window_error_func = custom_error_func;
  173.   WindowSaveInitial(active_video_page);
  174.   initialize_error();
  175.  
  176.   /* Initialize Direct memory writes, Snow check or BIOS screen updates
  177.    * depending on user's response */
  178.   CHECK_SNOW = d;
  179.   SCREEN_WRITE_METHOD = (d == 2)?BIOS:DMA;
  180.  
  181.   /* Save Initial Screen as a VBLOCK and Clear Screen */
  182.   v = VideoSave(1,1,num_screen_rows,num_screen_cols);
  183.   ClearScreen(NORM);
  184.  
  185.   /* Save What's on the screen now in a window - this gives us the base
  186.    * window to draw on */
  187.  
  188.   /* Initialize and open help window */
  189.   help = WindowInitialize(NOBORDER,25,1,80,1,BLACKONWHITE,BLACKONWHITE,0);
  190.   WindowOpen(help);
  191.   WindowWriteCenterString(help,
  192.   "Up \030   Down \031   Left \033   Right \032   Quit <CR>",1);
  193.  
  194.   /* Initialize and open main window */
  195.   main_window = WindowInitialize(BORDER, 1, 1, 78, 23,BLACKONWHITE,BLACKONWHITE, DOUBLEBOX);
  196.   WindowOpen(main_window);
  197.  
  198.   /* Initialize and open escape window */
  199.   esc_window = WindowInitialize(BORDER,22,54,24,2,WHITEONBLUE,WHITEONBLUE,SINGLEBOX);
  200.   WindowOpen(esc_window);
  201.   WindowPrintf(esc_window,"Press ESC to Quit\nAny other key to advance");
  202.  
  203.   /* Hide Cursor in main window */
  204.   WindowChangeCursor(main_window,INVISIBLE);
  205.  
  206.   /* Write Some Strings to main window */
  207.   WindowWriteCenterString(main_window, "THE C WINDOW LIBRARY DEMO", 1);
  208.   WindowWriteCenterString(main_window, "─────────────────────────", 2);
  209.   WindowTitleTop(main_window, title1, TITLECENTER);
  210.   i = 0;
  211.  
  212.   /* Display main window and escape window - make sure escape window is
  213.    * on top by making main window's rank = 2 and the escape window's
  214.    * rank = 1 */
  215.   WindowDisplay(main_window,2,NOEFFECT);
  216.   WindowDisplay(esc_window,1,NOEFFECT);
  217.  
  218.   /* Write info to main window */
  219.   while (screen1[i] != NULL)
  220.   {
  221.     WindowWriteString(main_window, screen1[i], i+3, 1);
  222.     i++;
  223.   }
  224.   i = 0;
  225.   approx_delay(4000);
  226.  
  227.   /* Write Slowly to main window */
  228.   while (delay_text[i] != NULL)
  229.   {
  230.     WindowWriteString(main_window, delay_text[i], i+9, 20);
  231.     i++;
  232.     approx_delay(2000);
  233.   }
  234.   approx_delay(15000);
  235.  
  236.   /* Clear text from a region of the main window */
  237.   WindowClearRegion(main_window, 3, 1, 23, 78);
  238.  
  239.   /* Call main demo() function */
  240.   demo();
  241.  
  242.   /* Dispose of main window */
  243.   WindowFree(main_window,NOEFFECT);
  244.  
  245.   /* Put original screen back and move cursor to startup position*/
  246. }
  247.  
  248.  
  249.  
  250. void demo()
  251. {
  252.   int i,j,k;
  253.   int dir;
  254.   char buf[50], buf2[50];
  255.   char *str = "You can also open multiple windows...";
  256.   buf[0] = 0;
  257.  
  258.   /* Initialize and open sub-window 1 */
  259.   w[0] = WindowInitialize(BORDER, 15, 20, 38, 5, CREATE_VIDEO_ATTRIBUTE(blue, lightwhite),
  260.                CREATE_VIDEO_ATTRIBUTE(blue, lightwhite), SINGLEBOX);
  261.   WindowOpen(w[0]);
  262.  
  263.  
  264.   WindowWriteString(main_window,
  265.         "You can choose different border styles ...", 6, 1);
  266.   approx_delay(2000);
  267.  
  268.  
  269.   /* Demonstrate drawing a new border for main window.  Also must rewrite
  270.    * title because title is destroyed when new border is written */
  271.  
  272.   WindowDrawBorder(main_window,DOUBLEBOX);
  273.   approx_delay(2000);
  274.   WindowDrawBorder(main_window,MIXEDBOX1);
  275.   approx_delay(2000);
  276.   WindowDrawBorder(main_window,BLANKBOX);
  277.   approx_delay(2000);
  278.   WindowDrawBorder(main_window,SINGLEBOX);
  279.   approx_delay(2000);
  280.  
  281.  
  282.   /* Change main window's border attribute */
  283.  
  284.   WindowWriteString(main_window,
  285.         "You can choose different border colors ...", 7, 1);
  286.   approx_delay(3000);
  287.   for (i=0;i<8;i++)
  288.   {
  289.     WindowChangeBorderAttribute(main_window,CREATE_VIDEO_ATTRIBUTE(i,lightwhite));
  290.     approx_delay(1000);
  291.   }
  292.   WindowChangeBorderAttribute(main_window,BLACKONWHITE);
  293.  
  294.  
  295.   /* Change main window's text attribute */
  296.  
  297.   WindowWriteString(main_window,
  298.         "You can also choose different window colors ...", 8, 1);
  299.   approx_delay(3000);
  300.   for (i=0;i<8;i++)
  301.   {
  302.     WindowChangeTextAttribute(main_window,CREATE_VIDEO_ATTRIBUTE(i,lightwhite));
  303.     approx_delay(1000);
  304.   }
  305.   WindowChangeTextAttribute(main_window,BLACKONWHITE);
  306.   approx_delay(1000);
  307.   WindowClearRegion(main_window,3,1,9,70);
  308.  
  309.  
  310.   /* Title sub window 1 and display it */
  311.  
  312.   WindowTitleTop(w[0],title2,TITLECENTER);
  313.   WindowDisplay(w[0],2,NOEFFECT);
  314.  
  315.   /* Write text to sub window */
  316.   WindowWriteString(main_window,"You can write text to a window...",3,1);
  317.   approx_delay(1000);
  318.   WindowWriteString(w[0],"Hello, world!",1,1);
  319.   approx_delay(2000);
  320.  
  321.   /* Demonstrate writing text with different colors */
  322.   WindowWriteString(main_window,
  323.      "You can write text with a different colors...",4,1);
  324.   approx_delay(2000);
  325.   WindowWriteStringAttr(w[0],"Hello, World!",2,1,BLACKONWHITE);
  326.   WindowWriteStringAttr(w[0],"Hello, World!",3,1,YELLOWONRED);
  327.   approx_delay(2000);
  328.  
  329.   /* Demonstrate WindowPrintf() */
  330.   WindowWriteString(main_window,"You can even use a printf() for windows!",5,1);
  331.   approx_delay(2000);
  332.   WindowWriteString(main_window,"To demonstrate, I will count to 20...",6,1);
  333.   approx_delay(2000);
  334.   WindowMoveCursor(w[0],4,1);
  335.   for (i=1;i<=20;i++)
  336.   {
  337.     WindowPrintf(w[0],"Count is now %d\n",i);
  338.     approx_delay(1000);
  339.   }
  340.   approx_delay(4000);
  341.   WindowClear(w[0]);
  342.  
  343.   /* Demonstrate right justification of strings in a window */
  344.   WindowWriteString(main_window,
  345.                   "You can write Right Justified strings...",7,1);
  346.   approx_delay(2000);
  347.   WindowWriteStringRJ(w[0],"These strings",1,19);
  348.   WindowWriteStringRJ(w[0],"are printed Right",2,19);
  349.   WindowWriteStringRJ(w[0],"justified!",3,19);
  350.   approx_delay(4000);
  351.  
  352.   /* Demonstrate centering strings around a column in a window */
  353.   WindowWriteString(main_window,
  354.       "You can write strings Centered on a certain column...",8,1);
  355.   approx_delay(2000);
  356.   WindowClear(w[0]);
  357.   WindowWriteStringCC(w[0],"These strings",1,10);
  358.   WindowWriteStringCC(w[0],"are centered on",2,10);
  359.   WindowWriteStringCC(w[0],"column 10",3,10);
  360.   approx_delay(3000);
  361.   WindowClear(w[0]);
  362.   WindowWriteStringCC(w[0],"These strings",1,19);
  363.   WindowWriteStringCC(w[0],"are centered on",2,19);
  364.   WindowWriteStringCC(w[0],"column 19",3,19);
  365.   approx_delay(3000);
  366.   WindowClear(w[0]);
  367.   WindowWriteStringCC(w[0],"These strings",1,29);
  368.   WindowWriteStringCC(w[0],"are centered on",2,29);
  369.   WindowWriteStringCC(w[0],"column 29",3,29);
  370.   approx_delay(3000);
  371.   WindowClearRegion(main_window,3,1,9,70);
  372.   WindowClear(w[0]);
  373.  
  374.   /* Move sub window around on the screen */
  375.   WindowWriteString(main_window,"You can move a window around...",3,1);
  376.   approx_delay(2000);
  377.   for (i=1;i<=10;i++)
  378.   {
  379.     WindowMove(w[0],rand()%15+1,rand()%60+1);
  380.     approx_delay(1000);
  381.   }
  382.   WindowMove(w[0],14,12);
  383.   approx_delay(2000);
  384.  
  385.   /* Slide sub window around on the screen */
  386.   WindowWriteString(main_window,"You can slide a window...",4,1);
  387.   WindowWriteString(main_window,"Press A key to Stop sliding...",5,1);
  388.   while (!ISKEYREADY())
  389.   {
  390.     dir = rand()%4+1;
  391.     if (dir == UP || dir == DOWN)
  392.       WindowSlide(w[0],dir,rand()%10+1);
  393.     else
  394.       WindowSlide(w[0],dir,rand()%30+1);
  395.   }
  396.   approx_delay(5000);
  397.   WindowMove(w[0],15,20);
  398.   WindowHide(w[0],NOEFFECT);
  399.   WindowClearRegion(main_window,3,1,9,70);
  400.  
  401.   /* Erase and display main window using special effects */
  402.   WindowWriteString(main_window,
  403.    "You can remove a window from the screen using special effects",3,1);
  404.   approx_delay(4000);
  405.   for (i=0;i<=11;i++)
  406.   {
  407.     WindowWriteString(main_window,effectstr[i],i+4,1);
  408.     approx_delay(2000);
  409.     WindowHide(main_window,i);
  410.     approx_delay(2000);
  411.       WindowDisplay(main_window,2,i);
  412.     approx_delay(2000);
  413.   }
  414.   WindowClearRegion(main_window,3,1,15,70);
  415.   WindowWriteString(main_window, str, 3, 1);
  416.   approx_delay(5000);
  417.  
  418.   /* Initialize, open, and display two more sub windows */
  419.   w[1] = WindowInitialize(BORDER, 12, 32, 38, 5, CREATE_VIDEO_ATTRIBUTE(green, lightwhite),
  420.   CREATE_VIDEO_ATTRIBUTE(green, lightwhite), SINGLEBOX);
  421.   WindowOpen(w[1]);
  422.   WindowWriteCenterString(w[1],title3,0);
  423.   w[2] = WindowInitialize(BORDER, 9, 38, 38, 5, BLACKONWHITE, BLACKONWHITE,
  424.                           SINGLEBOX);
  425.   WindowOpen(w[2]);
  426.   WindowTitleTop(w[2],title4,TITLECENTER);
  427.   WindowDisplay(w[2],2,NOEFFECT);
  428.   WindowDisplay(w[1],2,NOEFFECT);
  429.   WindowDisplay(w[0],2,NOEFFECT);
  430.   WindowWriteString(w[1],"This is window 2",1,1);
  431.   WindowWriteString(w[2],"This is window 3",1,1);
  432.   WindowWriteString(w[0],"This is window 1",1,1);
  433.   approx_delay(2000);
  434.  
  435.   /* Demonstrate writing text to any window at any time */
  436.   WindowWriteString(main_window,"You can write text to any window, at any time",4,1);
  437.   WindowWriteString(main_window,"regardless of which window is on top...",5,1);
  438.   approx_delay(3000);
  439.   write_all(w[0]);
  440.   approx_delay(2000);
  441.   write_all(w[1]);
  442.   approx_delay(2000);
  443.   write_all(w[2]);
  444.   approx_delay(2000);
  445.  
  446.  
  447.   /* Scroll text in sub windows to demonstrate independent scrolling */
  448.   WindowWriteString(main_window,"Press A key to stop scrolling...",7,1);
  449.   approx_delay(2000);
  450.   i = 1;
  451.   while (!ISKEYREADY())
  452.   {
  453.     WindowPrintf(w[0]," Scrolling line %d in window 1\n",i);
  454.     WindowPrintf(w[1]," Scrolling line %d in window 2\n",i);
  455.     WindowPrintf(w[2]," Scrolling line %d in window 3\n",i);
  456.     i++;
  457.   }
  458.   approx_delay(2000);
  459.   WindowWriteString(main_window,"You can change the order around...",8,1);
  460.   approx_delay(2000);
  461.   WindowHide(w[0],0);
  462.   WindowDisplay(w[0],2,0);
  463.   approx_delay(2000);
  464.   WindowDisplay(w[2],2,0);
  465.   approx_delay(2000);
  466.   WindowDisplay(w[1],2,0);
  467.   approx_delay(4000);
  468.  
  469.   /* Slide all of the sub windows */
  470.   slide_windows();
  471.   approx_delay(4000);
  472.  
  473.   /* Change the number of rows displayed on the screen */
  474.   change_screen_size();
  475.   WindowClearRegion(main_window,3,1,15,70);
  476.  
  477.   /* Take off top two sub-windows windows */
  478.   WindowWriteString(main_window,"Let's remove the top two windows..",3,1);
  479.   approx_delay(2000);
  480.   WindowHide(w[1],NOEFFECT);
  481.   approx_delay(1000);
  482.   WindowHide(w[2],NOEFFECT);
  483.   approx_delay(1000);
  484.   WindowClear(w[0]);
  485.  
  486.   /* Demonstrate see-thru window */
  487.   WindowWriteString(main_window,"You can create see-thru windows...",4,1);
  488.   approx_delay(2000);
  489.   WindowMakeSeeThru(w[0]);
  490.   approx_delay(2000);
  491.   WindowWriteString(main_window,
  492.     "Use Cursor Keys to move see-thru window around, RETURN to stop...",5,1);
  493.  
  494.  /* Slide this see-thru window around */
  495.   slide_see_thru_window(w[0]);
  496.  
  497.  /* Make see-thru windoqw a normal window again */
  498.   WindowMakeNormal(w[0]);
  499.   WindowClearRegion(main_window,3,1,15,70);
  500.   WindowWriteString(main_window,
  501.            "You can shadow window in 4 different positions...",3,1);
  502.   approx_delay(2000);
  503.  
  504.   /* Draw Shadow on all 4 possible positions of the window */
  505.   WindowDrawShadow(w[0],SHADOWUPLEFT,2,1,'█',BLACKONWHITE,0);
  506.   approx_delay(2000);
  507.   WindowDrawShadow(w[0],SHADOWUPRIGHT,2,1,'█',BLACKONWHITE,0);
  508.   approx_delay(2000);
  509.   WindowDrawShadow(w[0],SHADOWLOWLEFT,2,1,'█',BLACKONWHITE,0);
  510.   approx_delay(2000);
  511.   WindowDrawShadow(w[0],SHADOWLOWRIGHT,2,1,'█',BLACKONWHITE,0);
  512.   approx_delay(4000);
  513.   WindowClearRegion(main_window,3,1,15,70);
  514.   WindowClearRegion(main_window,20,14,21,70);
  515.   WindowRemoveShadow(w[0]);
  516.  
  517.   /* Demonstrate resizing sub-window */
  518.   WindowWriteString(main_window,"You can resize this window...",3,1);
  519.   approx_delay(4000);
  520.   resize_demo();
  521.  
  522.  
  523.   WindowWriteString(main_window,
  524.   "You can create windows larger than the physical screen ",3,1);
  525.   WindowWriteString(main_window,
  526.      "And scroll this window to see different sections",4,1);
  527.   approx_delay(5000);
  528.  
  529.   /* Initialize a virtual window */
  530.   vw = VirtualInitialize(NOATTRIBUTE,80,80,0);
  531.  
  532.   /* Read a file into the virtual window */
  533.   read_file();
  534.  
  535.   /* Assign viewport to virtual window */
  536.   WindowAssignToVirtual(w[0],vw,1,1);
  537.  
  538.   /* Make viewport larger */
  539.   WindowHide(w[0],NOEFFECT);
  540.   WindowMove(w[0],1,1);
  541.   WindowResizeWidth(w[0],78,ANCHORLEFT);
  542.   WindowResizeHeight(w[0],22,ANCHORTOP);
  543.   WindowDisplay(w[0],2,EXPLODE);
  544.   WindowDisplay(help,3,NOEFFECT);
  545.  
  546.   WindowHide(esc_window,NOEFFECT);
  547.   /* Turn off error checking temporarily */
  548.   error_check_flag = 0;
  549.   pan_window(w[0]);
  550.   /* Turn error checking back on */
  551.   error_check_flag = 1;
  552.   WindowDisplay(esc_window,1,NOEFFECT);
  553.  
  554.   /* Make viewport small again */
  555.   WindowHide(w[0],CONTRACT);
  556.   WindowHide(help,NOEFFECT);
  557.   WindowResizeWidth(w[0],38,ANCHORLEFT);
  558.   WindowResizeHeight(w[0],6,ANCHORTOP);
  559.   WindowMove(w[0],14,12);
  560.  
  561.   /* Make viewport into a normal window again */
  562.   WindowCloseViewport(w[0],0,NOEFFECT);
  563.   WindowClearRegion(main_window,3,1,15,70);
  564.   WindowMoveCursor(main_window,15,1);
  565.   WindowMove(w[0],14,1);
  566.   WindowResizeWidth(w[0],78,ANCHORLEFT);
  567.   WindowDisplay(w[0],2,0);
  568.  
  569.   /* Demonstrate input routines */
  570.   WindowWriteString(main_window,"You can also get input from windows...",3,1);
  571.   approx_delay(4000);
  572.   buf[0] = 0;
  573.   VirtualClear(vw);
  574.   WindowAssignToVirtual(w[0],vw,1,1);
  575.   WindowWriteString(w[0],"Enter Your First Name :",1,1);
  576.  
  577.   /* Get a string */
  578.   WindowGetString(w[0],2,5,buf,'_',1,15,VIRTUALFIELD,1,"30.");
  579.   WindowMoveCursor(w[0],3,1);
  580.   WindowPrintf(w[0],"You entered %s",buf);
  581.   approx_delay(10000);
  582.   WindowWriteString(main_window,
  583.            "You can get input with a different attribute...",4,1);
  584.   approx_delay(10000);
  585.  
  586.   /* Make sure input buffer's first character is null */
  587.   buf[0] = '\0';
  588.  
  589.   WindowClear(w[0]);
  590.   WindowWriteString(w[0],"Enter Your Last Name :",1,1);
  591.  
  592.   /* Get a string, but the attribute is reversed */
  593.   WindowGetStringAttr(w[0],2,1,buf,'_',1,15,0,1,BLACKONWHITE,"30.");
  594.   WindowMoveCursor(w[0],3,1);
  595.   WindowPrintf(w[0],"You entered %s",buf);
  596.   approx_delay(10000);
  597.   WindowWriteString(main_window,
  598.            "You can get input with an input mask ..",5,1);
  599.   buf[0] = 0;
  600.   WindowClear(w[0]);
  601.  
  602.   /* Get a string using a character mask */
  603.   WindowWriteString(w[0],"Enter Your Social Security Number :",1,1);
  604.   WindowGetMaskString(w[0],2,1,buf,"___-__-____",'_',1,15,0,1,"9[0-9]");
  605.   WindowMoveCursor(w[0],3,1);
  606.  
  607.   /* Use mask pattern to format output string */
  608.   TranslateStringToMaskString(buf,"___-__-____",buf2,'_',1);
  609.   WindowPrintf(w[0],"You entered %s",buf2);
  610.   approx_delay(10000);
  611.   buf[0] = 0;
  612.   WindowWriteString(main_window,
  613.     "You can get input with an input mask and have a different attribute..",6,1);
  614.   approx_delay(10000);
  615.   WindowClear(w[0]);
  616.  
  617.  
  618.   /* Get a string using a character mask, but use different attribute */
  619.   WindowWriteString(w[0],"Enter Your Social Security Number :",1,1);
  620.   WindowGetMaskStringAttr(w[0],2,1,buf,"___-__-____",'_',1,15,0,1,BLACKONWHITE,"9[0-9]");
  621.   WindowMoveCursor(w[0],3,1);
  622.  
  623.   /* Use mask pattern to format output string */
  624.   TranslateStringToMaskString(buf,"___-__-____",buf2,'_',1);
  625.   WindowPrintf(w[0],"You entered %s",buf2);
  626.   approx_delay(10000);
  627.   buf[0] = 0;
  628.   WindowWriteString(main_window,"You can get password input...",7,1);
  629.   approx_delay(10000);
  630.   WindowClear(w[0]);
  631.   WindowWriteString(w[0],"Enter Your Name :",1,1);
  632.  
  633.   /* Get password input */
  634.   WindowGetPassword(w[0],2,1,buf,'_','*',1,15,0,1,"30.");
  635.   WindowMoveCursor(w[0],3,1);
  636.   WindowPrintf(w[0],"You entered %s",buf);
  637.   approx_delay(10000);
  638.   buf[0] = 0;
  639.   WindowWriteString(main_window,
  640.      "You can get password input with a different attribute..",8,1);
  641.   approx_delay(10000);
  642.   WindowClear(w[0]);
  643.   WindowWriteString(w[0],"Enter Your Name :",1,1);
  644.  
  645.   /* Get password but use different video attribute */
  646.   WindowGetPasswordAttr(w[0],2,1,buf,'_','*',1,15,0,1,BLACKONWHITE,"30.");
  647.   WindowMoveCursor(w[0],3,1);
  648.   WindowPrintf(w[0],"You entered %s",buf);
  649.   approx_delay(10000);
  650.  
  651.   /* Hide all of the windows */
  652.   WindowHide(w[0],NOEFFECT);
  653.   WindowHide(main_window,NOEFFECT);
  654.   WindowHide(esc_window,NOEFFECT);
  655.  
  656.   /* Clear the screen */
  657.   ClearScreen(NORM);
  658.  
  659.   /* Demonstrate writing to the screen without using window functions */
  660.   VideoWriteString(
  661.       "You also have non-window related, fast video functions.",1,1);
  662.   VideoWriteString(
  663. "To demonstrate, the normal printf() function will be used to fill the screen",2,1);
  664.   VideoWriteString("Press a key to start demonstration...",4,1);
  665.   GET_KEY();
  666.  
  667.   /* Move Cursor to (1,1) */
  668.   MoveCursor(1,1,0);
  669.  
  670.   /* Loop until all rows are filled with our message using normal printf() */
  671.   for (i=0; i<num_screen_rows; i++)
  672.     printf("Using the normal printf() function call\n");
  673.   printf("Press a key to demonstrate the C Window Libraries fast video ...");
  674.   GET_KEY();
  675.  
  676.   /* Clear the screen */
  677.   ClearScreen(NORM);
  678.  
  679.   /* Move Cursor to (1,1) */
  680.   MoveCursor(1,1,0);
  681.  
  682.   /* Do same thing as before, but use VideoPrintf() */
  683.   for (i=0;i<num_screen_rows;i++)
  684.     VideoPrintf(
  685.           "Using the C Window Libraries VideoPrintf() function call\n");
  686.   approx_delay(20000);
  687.   last_function();
  688. }
  689.  
  690. void last_function()
  691. {
  692.   int i,j,k;
  693.   WindowFree(esc_window,NOEFFECT);
  694.   WindowClear(main_window);
  695.  
  696.   /* Write closing message */
  697.   WindowWriteCenterString(main_window,closing_message[0],5);
  698.   for (i=2;i<=9;i++)
  699.     WindowWriteString(main_window,closing_message[i],i+6,1);
  700.   for (i=10;i<=13;i++)
  701.     WindowWriteCenterString(main_window,closing_message[i],i+6);
  702.   for (i=0;i<2;i++)
  703.     if (ISOPEN(w[i]))
  704.       WindowFree(w[i],NOEFFECT);
  705. /*  WindowDisplay(main_window,2,NOEFFECT);*/
  706.   i = 1;
  707.  
  708.   /* Change video attribute until a key is pressed for the desired effect */
  709.   while(!ISKEYREADY())
  710.   {
  711.     j = i % 16;
  712.     if (j == 0)
  713.       j = 1;
  714.     k =
  715.   CREATE_VIDEO_ATTRIBUTE(GET_BGROUND_COLOR(WINDOW_TEXT_COLOR(main_window)),j);
  716.     WindowWriteCenterStringAttr(main_window,closing_message[1],6,k);
  717.     WindowWriteCenterStringAttr(main_window,closing_message[15],21,k);
  718.     i++;
  719.     delay(10);
  720.   }
  721.   VideoRestore(v);
  722.   MoveCursor(initial_video_startup.cursor_row,
  723.              initial_video_startup.cursor_col,0);
  724.  
  725.   /* Change cursor back to original shape */
  726.   ChangeCursor(initial_video_startup.cursor_startline,
  727.                initial_video_startup.cursor_endline);
  728.   exit(0);
  729. }
  730.  
  731.  
  732. void resize_demo(void)
  733. /************************************************************************
  734.  * This demo demonstrates the resizing features of the C Window Library *
  735.  ***********************************************************************/
  736. {
  737.   int i;
  738.  
  739.   /* Get original width and height */
  740.   int h = WINDOW_HEIGHT(w[0]);
  741.   int wid = WINDOW_WIDTH(w[0]);
  742.  
  743.  
  744.   /* Resize Height of window.  Also rewrite title because border text is
  745.    * destroyed when window is resized */
  746.   for (i=h+2;i<=17;i+=2)
  747.   {
  748.     WindowResizeHeight(w[0],i,ANCHORBOTTOM);
  749.     approx_delay(200);
  750.   }
  751.   for (i=17;i>=h;i-=2)
  752.   {
  753.     WindowResizeHeight(w[0],i,ANCHORTOP);
  754.     approx_delay(200);
  755.   }
  756.   for (i=h+2;i<=17;i+=2)
  757.   {
  758.     WindowResizeHeight(w[0],i,ANCHORTOP);
  759.     approx_delay(200);
  760.   }
  761.   for (i=17;i>=h;i-=2)
  762.   {
  763.     WindowResizeHeight(w[0],i,ANCHORBOTTOM);
  764.     approx_delay(200);
  765.   }
  766.  
  767.  
  768.   /* Resize width of window.  Also rewrite title because border text is
  769.    * destroyed when window is resized */
  770.  
  771.   for (i=wid+2;i<=60;i+=3)
  772.   {
  773.     WindowResizeWidth(w[0],i,ANCHORLEFT);
  774.     approx_delay(200);
  775.   }
  776.   for (i=60;i>=wid;i-=3)
  777.   {
  778.     WindowResizeWidth(w[0],i,ANCHORRIGHT);
  779.     approx_delay(200);
  780.   }
  781.   for (i=wid+2;i<=60;i+=3)
  782.   {
  783.     WindowResizeWidth(w[0],i,ANCHORRIGHT);
  784.     approx_delay(200);
  785.   }
  786.   for (i=60;i>=wid+2;i-=3)
  787.   {
  788.     WindowResizeWidth(w[0],i,ANCHORLEFT);
  789.     approx_delay(200);
  790.   }
  791. }
  792. /**********************************************************
  793.  * Gets a filename and displays it in the virtual window  *
  794.  *********************************************************/
  795. read_file()
  796. {
  797.     int ch;
  798.     unsigned r = 1, c = 1;
  799.     infile = fopen("demo.c","r");  /* read file name into window */
  800.     while(1)
  801.     {
  802.       ch = fgetc(infile);          /* get a character */
  803.       if (feof(infile))
  804.         break;
  805.       if (ch != '\n' && c <= 80)   /* check if safe to write to virtual
  806.                                     * window  */
  807.         VirtualWriteRepeatCharacter(vw,ch,r,c++,1); /* write character to vw */
  808.       else
  809.       {
  810.         if (c > 80)
  811.        /* read until '\n' is reached */
  812.           while((ch = fgetc(infile)) != '\n')
  813.          ;
  814.         r++;                    /* increment row */
  815.         if (r > 80) break;
  816.           c=1;
  817.       }
  818.     }
  819.     fclose(infile);             /* close input file */
  820.     return 0;
  821. }
  822.  
  823.  
  824. /* Write alphabet to window */
  825. write_all(WPOINTER win)
  826. {
  827.   int i;
  828.   for (i = 1; i <= WINDOW_HEIGHT(win); i++)
  829.     WindowWriteString(win,"ABCDEFGHIJKLMNOPQRSTUVWXYZ",i,1);
  830.   return 0;
  831. }
  832.  
  833.  
  834.  
  835. /******************************
  836.  * Slide window demonstration *
  837.  *****************************/
  838. void slide_see_thru_window(WPOINTER win)
  839. {
  840.   int ch = 0;
  841.   /* Keep going until RETURN key is pressed */
  842.   while(ch != RETURN)
  843.   {
  844.     /* Get a key and slide 1 character in the desired direction */
  845.     switch(ch = GET_KEY())
  846.     {
  847.       case UARROW:
  848.         WindowSlide(win,UP,1);
  849.       break;
  850.       case DARROW:
  851.         WindowSlide(win,DOWN,1);
  852.       break;
  853.       case LARROW:
  854.         WindowSlide(win,LEFT,1);
  855.       break;
  856.       case RARROW:
  857.         WindowSlide(win,RIGHT,1);
  858.       break;
  859.       case 13:
  860.         WindowMove(win,14,12);
  861.       break;
  862.     }
  863.   }
  864. }
  865.  
  866.  
  867. /*********************************
  868.  * Scroll viewport demonstration *
  869.  ********************************/
  870. pan_window(WPOINTER win)
  871. {
  872.   int ch = 0;
  873.  
  874.   /* keep going until Escape or Return key is pressed */
  875.   /* Error checking is temporarily disabled because you may attempt to scroll
  876.    * beyond the bounds of the window */
  877.  
  878.   while(ch != ESC && ch != RETURN)
  879.   {
  880.     switch(ch = GET_KEY())
  881.     {
  882.       case UARROW:
  883.         WindowScrollViewport(win,1,UP);
  884.       break;
  885.       case DARROW:
  886.         WindowScrollViewport(win,1,DOWN);
  887.       break;
  888.       case LARROW:
  889.         WindowScrollViewport(win,1,LEFT);
  890.       break;
  891.       case RARROW:
  892.         WindowScrollViewport(win,1,RIGHT);
  893.       break;
  894. /*      case ESC:
  895.         VideoRestore(v);
  896.       break;            */
  897.       case RETURN:
  898.       break;
  899.     }
  900.   }
  901. }
  902.  
  903.  
  904. /* Delay function that either exits when you press a key, or when time
  905.  * runs out.  Demo is terminated if ESC key is pressed  */
  906.  
  907. approx_delay(int mil)
  908. {
  909.   unsigned start = 5;
  910.   int ch;
  911.  
  912.   /* quit if key is in keyboard buffer */
  913.   while(!ISKEYREADY())
  914.   {
  915.     delay(10);
  916.     start+=10;
  917.   /* return because time ran out */
  918.     if (start > mil)
  919.       return 0;
  920.   }
  921.   /* read the key */
  922.   ch = GET_KEY();
  923.  
  924.   /* quit demo if the Escape key was pressed */
  925.   if (ch == ESC)
  926.   {
  927.     last_function();
  928.   }
  929.   return 0;
  930. }
  931.  
  932.  
  933.  
  934. /**********************************************************************
  935.  * Changes screen rows.  This routine exits if the hardware dowes not *
  936.  * support screen rows > 25 lines.                                    *
  937.  *********************************************************************/
  938. int change_screen_size()
  939. {
  940.   int rows,oldrows = num_screen_rows;
  941.   WindowClearRegion(main_window,3,1,15,70);
  942.   WindowWriteString(main_window,"You can change screen size...",5,1);
  943.   approx_delay(5000);
  944.  
  945.   /* Check for VGA mode */
  946.   if (VGA_MODE_ON)
  947.     SetVideoRows(rows=50,0);
  948.  
  949.   else
  950.  
  951.   /* Check for EGA or MCGA mode */
  952.   if (EGA_MODE_ON || MCGA_MODE_ON)
  953.     SetVideoRows(rows=43,0);
  954.  
  955.   /* Video cannot change rows, so exit */
  956.   else
  957.   {
  958.     WindowWriteString(main_window,
  959.   "Sorry, your video adapter does not support more than 25 rows...",6,1);
  960.     approx_delay(8000);
  961.     return 0;
  962.   }
  963.  
  964.   /* Adjust all global data in The C Window Library to reflect the new
  965.    * number of rows */
  966.   AdjustScreenInfo(0);
  967.  
  968.   /* Resize the height of the main window */
  969.   WindowResizeHeight(main_window,rows-2,ANCHORTOP);
  970.   WindowWriteCenterString(main_window,title1,0);
  971.  
  972.   /* Slide all sub-windows */
  973.   slide_windows();
  974.   WindowWriteString(main_window,"Press a key to change back to 25 rows",6,1);
  975.   GET_KEY();
  976.  
  977.   /* Resize window.  This must be done so that the window will fit in a
  978.    * 25 row screen (or whatever the old number of video rows were) */
  979.   WindowResizeHeight(main_window,oldrows-2,ANCHORTOP);
  980.   WindowWriteCenterString(main_window,title1,0);
  981.   SetVideoRows(oldrows,0);
  982.  
  983.   /* Readjust global data */
  984.   AdjustScreenInfo(0);
  985.   return 0;
  986. }
  987.  
  988.  
  989. /* Slide all 3 sub-windows randomly */
  990. slide_windows()
  991. {
  992.   int i,j,k;
  993.   WindowWriteString(main_window,"Press a key to stop sliding...",9,1);
  994.   approx_delay(2000);
  995.   while (!ISKEYREADY())
  996.   {
  997.     i = rand()%3;
  998.     j = rand()%4+1;
  999.     if (j == UP || j == DOWN)
  1000.       k = rand()%15+1;
  1001.     else
  1002.       k = rand()%50+1;
  1003.     WindowSlide(w[i],j,k);
  1004.   }
  1005.   WindowMove(w[0],15,20);
  1006.   WindowMove(w[1],14,14);
  1007.   WindowMove(w[2],16,24);
  1008.   return 0;
  1009. }
  1010.  
  1011.  
  1012. void initialize_error()    /* Initialize error window and error handler */
  1013. {
  1014.   error_window =
  1015.     WindowInitialize(BORDER,1,1,60,5,WHITEONRED,WHITEONRED,SINGLEBOX);
  1016.   WindowOpen(error_window);
  1017.   window_error_func = custom_error_func;  /* Assign error function */
  1018. }
  1019.  
  1020.  
  1021. void custom_error_func(int errcode, char *sourcefile, int sourceline,
  1022.                char *funcname)
  1023. {
  1024.   int ch;
  1025.   if (error_check_flag)
  1026.   {
  1027.     MakeSound(100,500);           /* Beep */
  1028.     if (errcode == NO_HEAP_MEM)   /* Always check for the heap memory error
  1029.                    separately */
  1030.     {
  1031.       VideoWriteString("You have ran out of Heap Memory",1,1);
  1032.       VideoWriteString(
  1033.          "Press 'Q' to quit program, Any other key to Continue...",2,1);
  1034.     }
  1035.     else                         /* Display error window */
  1036.     {
  1037.       WindowClear(error_window);
  1038.       WindowDisplay(error_window,1,EXPLODE);
  1039.       WindowPrintf(error_window,
  1040.        "Error Code: %d\nSource File: %s\nSource Line %d\nError Occurred in %s",
  1041.        errcode,sourcefile,sourceline,funcname);
  1042.       WindowWriteString(error_window,
  1043.     "Press 'Q' to quit program, Any other key to Continue...",5,1);
  1044.     }
  1045.     ch = GET_KEY();
  1046.     if (ch == 'Q' || ch == 'q')
  1047.     {
  1048.       VideoRestore(v);
  1049.       exit(0);
  1050.     }
  1051.     else
  1052.     if (errcode != NO_HEAP_MEM)
  1053.       WindowHide(error_window,CONTRACT);
  1054.   }
  1055. }
  1056.